package idv.csc.contact.controller;

import idv.csc.common.controller.BaseController;
import idv.csc.contact.service.ContactService;
import idv.csc.contact.vo.ContactVO;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author Jeff Chang Contact Controller
 */
@Controller
public class ContactController extends BaseController {

	@Autowired
	private ContactService contactService;

	@RequestMapping("/contact/index")
	public String listContacts(Map<String, Object> map) {
		map.put("contactVO", new ContactVO());
		map.put("contactList", contactService.listContact());
		return "contact/contact";
	}

	@RequestMapping(value = "/contact/add", method = RequestMethod.POST)
	public String addContact(@ModelAttribute("contactVO") ContactVO contactVO, BindingResult result) {
		System.out.println("attr1=" + contactVO.getAttr1());
		contactService.addContact(contactVO.getContactPO());
		return "redirect:/contact/index.html";
	}

	@RequestMapping(value = "/contact/getAttr")
	public String getAttr(@RequestParam("attr1") int attr1) {
		System.out.println("attr1=" + attr1);
		return "redirect:/contact/index.html";
	}

	@RequestMapping("/contact/delete/{contactId}")
	public String deleteContact(@PathVariable("contactId") Integer contactId) {
		contactService.removeContact(contactId);
		return "redirect:/contact/index.html";
	}
}
